//
//  GameScene.cpp
//  p1
//
//  Created by yoona park on 13. 9. 29..
//
//

#include "GameScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* GameScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    GameScene *layer = GameScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool GameScene::init()
{
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    
    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "CloseNormal.png",
                                                          "CloseSelected.png",
                                                          this,
                                                          menu_selector(GameScene::menuCallback) );
    
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);
    
    /////////////////////////////
    // 3. add your codes below...
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    float rate = 0.5f;
    float width = 960;
    float height = 640;
    
    CCNode *baseNode = CCNode::create();
    baseNode->setAnchorPoint(ccp(0.5, 0.5));
    baseNode->setContentSize(CCSizeMake(width, height));
    baseNode->setPosition( ccp(size.width/2, size.height/2) );
    baseNode->setScale(rate/* *(1.0f/0.75f) */);
    baseNode->setVisible(true);
    this->addChild(baseNode);
    
    CCSprite* pSprite = CCSprite::create("image/main_game.png");
    pSprite->setContentSize( CCSize( width, height ));
    pSprite->setPosition( ccp(width/2, height/2) );
    baseNode->addChild(pSprite, 0);
    
    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Life Game", "Thonburi", 20);
    pLabel->setPosition( ccp(size.width / 2, size.height - 80) );
    pLabel->setColor( ccBLUE );
    this->addChild(pLabel, 1);
    
    //주사위 이미지
    dieceImg_1 = CCSprite::create("image/diece_1.jpg");
    dieceImg_1->setPosition( ccp(size.width/2-60, size.height/2) );
    this->addChild(dieceImg_1, 0);
    
    dieceImg_2 = CCSprite::create("image/diece_1.jpg");
    dieceImg_2->setPosition( ccp(size.width/2+60, size.height/2) );
    this->addChild(dieceImg_2, 0);
    
    //주사위 에니메이션 생성
    CCArray* animFrames = CCArray::create();
    for(int i = 0; i < 6; i++)
    {
        char imageName[30];
        sprintf(imageName, "image/diece_%i.jpg", i+1);
        
        CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(CCTextureCache::sharedTextureCache()->addImage(imageName), CCRectMake(0, 0, 100, 100));
        animFrames->addObject(frame);
    }
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 2.0f);
    CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "ani_diece");
    
    return true;
}

void GameScene::menuCallback(CCObject* pSender)
{
    
    CCAnimate *dieceAni = CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName("ani_diece"));
    dieceAni->setDuration(1.0f);
    dieceImg_1->runAction(CCRepeatForever::create( dieceAni ));
    dieceImg_2->runAction(CCRepeatForever::create( dieceAni ));
    
    //exitApp();
    //delayCCCallFunc(3.0f, callfunc_selector( GameScene::resultDiece) );
}

void GameScene::resultDiece()
{
    int rand_1 = random()%6+1;
    int rand_2 = random()%6+1;
    
    CCLog("rand: %i, %i", rand_1, rand_2);
    
    dieceImg_1->stopAllActions();
    dieceImg_2->stopAllActions();
    
    char imageName[40];
    sprintf( imageName, "image/diece_%d.jpg", rand_1);
    dieceImg_1->setTexture( CCTextureCache::sharedTextureCache()->addImage(imageName));
    sprintf( imageName, "image/diece_%d.jpg", rand_2);
    dieceImg_2->setTexture( CCTextureCache::sharedTextureCache()->addImage(imageName));
    
}

